package com.future.hedongjun.mine.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.future.baselib.activity.BaseActivity;
import com.future.baselib.entity.BaseResponse;
import com.future.baselib.entity.DefaultResponse;
import com.future.baselib.http.HttpRequest;
import com.future.baselib.utils.PatternUtils;
import com.future.hedongjun.R;
import com.future.hedongjun.common.MyApp;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class EditPhone2Activity extends BaseActivity {

    @BindView(R.id.et_mobile)
    EditText etMobile;
    @BindView(R.id.btn_getCode)
    Button btnGetCode;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.btn_ok)
    Button btnOk;
    private String codeNewPhone;
    private String mobile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_phone2);
        ButterKnife.bind(this);
        setTitle("修改绑定手机号");
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
    }

    @OnClick({R.id.btn_getCode, R.id.btn_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_getCode:
                getCode();
                break;
            case R.id.btn_ok:
                changePhone();
                break;
        }
    }

    private void changePhone() {
        String mobile = etMobile.getText().toString().trim();
        String codeInput = etCode.getText().toString().trim();
        if (!PatternUtils.mobilePattern(toast, mobile)) {
            return;
        }
        new HttpRequest<DefaultResponse>()
                .with(this)
                .setPath("/api/User/changemobile")
                .addParam("token", MyApp.getInstance().getUserInfo().getToken())
                .addParam("mobile",mobile)
                .addParam("captcha",codeInput)
                .addSuccessListener(new HttpRequest.OnNetworkSuccess() {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        startActivity(UserInfoActivity.class);
                    }
                }).start(new DefaultResponse());
    }

    private void getCode() {
        mobile = etMobile.getText().toString().trim();
        if (!PatternUtils.mobilePattern(toast, mobile)) {
            return;
        }
        btnGetCode.setClickable(false);
        new HttpRequest<DefaultResponse>()
                .with(this)
                .setPath("/api/Sms/send")
                .addParam("mobile", mobile)
                .addParam("event","changemobile")
                .addSuccessListener(new HttpRequest.OnNetworkSuccess() {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        new Thread(runnable).start();
                    }
                })
                .addFailListener(new HttpRequest.OnNetworkFail() {
                    @Override
                    public void onFail(String message) {
                        toast(message);
                        btnGetCode.setClickable(true);
                    }
                }).start(new DefaultResponse());
    }

    //验证码发送倒计时
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    btnGetCode.setClickable(false);
                }
            });
            for (int i = 60; i > 0; i--) {
                final int finalI = i;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        btnGetCode.setText("重新获取(" + finalI + ")");
                    }
                });
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    btnGetCode.setClickable(true);
                    btnGetCode.setText("重新获取");
                }
            });
        }
    };
}
